package cn.lzy.head.controller.headcontorller;

import cn.lzy.common.copy.BeanCopierUtil;
import cn.lzy.common.redis.RedisServie;
import cn.lzy.head.controller.dto.HeadDTO;
import cn.lzy.head.controller.vo.HeadVO;
import cn.lzy.head.service.Domain.HeadDO;
import cn.lzy.head.service.HeadService;

import cn.lzy.head.service.bo.HeadBO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/head")

public class HeadController {

    @Autowired
    private HeadService headService;

    @Autowired
    private RedisServie redisServie;


    /**
     * 功能描述: 头部功能区链接用户点击的方法
     *   里面有点击一次的方法，
     *   也有批量增加的方法，批量增加时，需要前台传入一共点击了多少次
     * @auther: lzy
     * @param null:
     * @date: 2021/7/17 11:20
     * @return:
     */

    @PostMapping("/autoadd")
    public void autoAdd(String head_name,Integer sum){
        //没有传入点击次数，单次增加
        Object obj = redisServie.getObj(head_name);
        if(0 == sum){
            redisServie.increment(head_name,1);
        }
        //传入了点击多少次的方法
        if(sum != 0){
            redisServie.increment(head_name,sum);
        }
    }


    /**
     * 功能描述: 获得头部全部信息
     * @auther: lzy

     * @date: 2021/7/17 9:20
     * @return: java.util.List<cn.lzy.head.controller.vo.HeadVO>
     */

    @GetMapping("/all")
    public List<HeadVO> getAll(){
        List<HeadDO> headFunction = headService.getHeadFunction();
        List<HeadVO> headVOS = BeanCopierUtil.copyList(headFunction, HeadVO.class);
        return headVOS;
    }
    /**
     * 功能描述: 新增头部信息
     * @auther: lzy
     * @param headDTO:
     * @date: 2021/7/17 9:40
     * @return: int
     */

    @PostMapping("/add")
    public int addHead(HeadDTO headDTO){
        HeadBO headBO = BeanCopierUtil.copyObj(headDTO, HeadBO.class);
        int add = headService.addHead(headBO);
        return add;
    }

    /**
     * 功能描述: 删除头部信息
     * @auther: lzy
     * @param headDTO:
     * @date: 2021/7/17 9:45
     * @return: int
     */
    @PostMapping("/del")
    public int delHead(HeadDTO headDTO){
        HeadBO headBO = BeanCopierUtil.copyObj(headDTO, HeadBO.class);
        int i = headService.delHead(headBO);
        return i;
    }
    /**
     * 功能描述: 修改头部信息
     * @auther: lzy
     * @param headDTO:
     * @date: 2021/7/17 10:05
     * @return: int
     */
    @PostMapping("/edit")
    public int editHead(HeadDTO headDTO){
        HeadBO headBO = BeanCopierUtil.copyObj(headDTO, HeadBO.class);
        int i = headService.editHead(headBO);
        return i;
    }
}
